package com.mychoize.app.cricscore;

import java.util.HashMap;
import java.util.Map;

import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;


public class MemCacheHelper {
	
	private static MemCacheHelper singleton;
	
	private static final String LIVESCORE  = "LIVESCORE";
	private static final int TIMEOUT_SECONDS = 5;
	
	private Cache cache;
	
	public static MemCacheHelper getInstane(){
		if(singleton == null || singleton.cache == null){
			singleton = new MemCacheHelper();
		}
		return singleton;
	}
	
	
	@SuppressWarnings("unchecked")
	public MemCacheHelper(){
		
		@SuppressWarnings("rawtypes")
		Map properties = new HashMap();
		properties.put(GCacheFactory.EXPIRATION_DELTA, TIMEOUT_SECONDS);

        try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(properties);
        } catch (CacheException e) {
            //e.printStackTrace();
        }

	}
	
	public SimpleScore getMatch(int key){
		return (SimpleScore) cache.get(key);
	}
	
	public void putMatch(SimpleScore score){
	    cache.put(score.getId(), score);
	}
	
	public String getLiveScore(){
		return (String)cache.get(LIVESCORE);
	}
	
	public void setLiveScore(String str){
		cache.put(LIVESCORE, str);
	}
	
}
